package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest implements Callable<Integer> {

    //callable接口 实现线程要执行的内容  返回值是integer，但不局限此类型
    public Integer call() throws Exception {
        System.out.println("开始执行线程："+Thread.currentThread().getName());
        int count =0;
        for(int i=0;i<10;i++){
            count ++;
            System.out.println("count:"+count);
        }
        System.out.println("结束执行线程："+Thread.currentThread().getName());
        return 99;
    }

    public static void main(String[] args) {

        try {
            CallableTest callableTest =new CallableTest();
            //接受线程调用后的返回值
            FutureTask<Integer> task =new FutureTask<Integer>(callableTest);
            //start 启动线程
            new Thread(task).start();
            //得到返回值 线程执行的结果
            Integer num = task.get();
            System.out.println(Thread.currentThread().getName()+"得到执行结果为="+num);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
